package jdklearn.nio.写文件;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * create at 2018/10/23
 * author: suveng
 * email: suveng@163.com
 **/
public class Main {
    public static void main(String[] args) throws IOException {
//        获取通道
        FileOutputStream fileOutputStream=new FileOutputStream("testnio");
        FileChannel channel = fileOutputStream.getChannel();
//        创建缓冲区，将数据写入缓冲区
        for (int i = 0; i < 10; i++) {
            ByteBuffer buffer=ByteBuffer.allocate(1024);
            for (int j = 0; j < 512; j++) {
                buffer.put((byte) 'n');
                buffer.put((byte) 'a');
            }
            buffer.flip();
//        把缓冲区写入通道中
            channel.write(buffer);
            buffer.clear();
        }


    }
}
